package com.winchain.chainbill.bill.biz.discount.cmd;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.SmsBillAprvPassArtisanKeyword;
import com.shell.governor.modules.message.keyword.SmsBillAprvPassKeyword;
import com.shell.governor.modules.message.keyword.SysDctAprvPassBillKeyword;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.util.Calendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctAprvSmsCmd implements ICommand<Bill<CbDct>>, ICallback<Bill<CbDct>, AcctTxRec> {
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private CbRecService cbRecService;
	@Autowired
	private CbSndService cbSndService;

	public boolean execute(CommandContext<Bill<CbDct>> context) throws Exception {
		CbDct cbDct = (CbDct) ((Bill) context.getSource()).getSource();
		if ((cbDct != null) && (cbDct.getDctId() != null)) {
			CbRec rec = ((Bill) context.getSource()).getRec();
			CusRec artisan = this.cusRecService.getArtisan(null);
			if (artisan == null) {
				throw new UnCheckMsgException("��������������������������");
			}
			AcctBas artisanAcct = this.billAcctService.getAcctBas(artisan.getCusId(), null, "00");

			String template = "MT02624";
			SmsBillAprvPassKeyword keyWord = new SmsBillAprvPassKeyword();
			keyWord.setDctAmt(String.valueOf(cbDct.getDctAmt()));
			keyWord.setBillCode(rec.getBillCode());
			keyWord.setSerFee(MoneyUtil.format(cbDct.getSerFee()));
			keyWord.setAcctNo(artisanAcct.getAcctNo());

			String senderId = CommonDataUtil.getUserId();
			MessageSender.sendMessageByTmpl(senderId, template, keyWord, cbDct.getSellerId());

			template = "MT02625";
			SmsBillAprvPassArtisanKeyword artisanKeyword = new SmsBillAprvPassArtisanKeyword();
			artisanKeyword.setCusName(cbDct.getSellerNm());
			artisanKeyword.setAcctName(cbDct.getSellerNm());
			artisanKeyword.setDctAmt(String.valueOf(cbDct.getDctAmt()));
			artisanKeyword.setBillCode(rec.getBillCode());
			artisanKeyword.setAcctNo(cbDct.getSellerAcct());
			MessageSender.sendMessageByTmpl(senderId, template, artisanKeyword, artisan.getCusId());

			String templateSec = "MT01615";
			CbRec cbRec = (CbRec) this.cbRecService.selectByKey(cbDct.getBillId());
			CbSnd cbSend = this.cbSndService.getBillSndById(cbRec.getRootId());
			SysDctAprvPassBillKeyword keyWordSec = new SysDctAprvPassBillKeyword();
			keyWordSec.setBillAmt(String.valueOf(cbDct.getDctAmt()));
			keyWordSec.setBillAmtA(MoneyUtil.number2CNMoney(cbDct.getDctAmt()));
			keyWordSec.setBillCode(cbRec.getBillCode());
			keyWordSec.setBuyerNm(cbSend.getBuyerNm());
			keyWordSec.setSellerNm(cbDct.getFinNm());
			Calendar c = Calendar.getInstance();
			keyWordSec.setXfeNm(cbDct.getSellerNm());
			keyWordSec.setYear(String.valueOf(c.get(1)));
			keyWordSec.setMonth(String.valueOf(c.get(2) + 1));
			keyWordSec.setDay(String.valueOf(c.get(5)));

			MessageSender.sendMessageByTmpl(senderId, templateSec, keyWordSec, cbSend.getBuyerId());
		}
		return false;
	}

	public boolean callback(CallbackContext<Bill<CbDct>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}
